<?php

// Model:'CuocKiemTra' - Database Table: 'doan_tt'

Class CuocKiemTra extends Eloquent {

    //Cac bang tham gia trong model
    private $tables = array("cuoc_tt", "ct_doan_tt", "vcuoctt");
    //Rang buoc du lieu
    private $rules = array(
                    "ID_DOAN_TT" => "required",
                    "ID_CS_TT" => "required",
                    "ID_NAM" => "required",
                    "THOI_KY_TT" => "required|min:1|max:200",
                    "NOI_DUNG_PC" => "required|min:1|max:500"
    );
    //So doan kiểm tra hien thi tren mot trang
    private $cuockt = 6;

    //Ham kiem tra du lieu
    public function validation($data) {
        return Validator::make($data, $this->rules);
    }

    //Lay so du lieu hien thi tren trang
    public function GETCKT() {
        return $this->cuockt;
    }

    //Lay ds du lieu
    public function GetList($key) {
        $data = null;

        if ($key === null) {
            $data = DB::table($this->tables[2])
                    ->select("*")
                    ->orderBy("ID")
                    ->paginate($this->cuockt);
        } else {
            $data = DB::table($this->tables[2])
                    ->select("*")
                    ->where("MST", "LIKE", "%" . $key . "%")
                    ->orWhere("MA_DOAN_TT", "LIKE", "%" . $key . "%")
                    ->orWhere("NAM", "LIKE", "%" . $key . "%")
                    ->orderBy("ID")
                    ->paginate($this->cuockt);
        }

        return $data;
    }

    //Lay du lieu xac dinh
    public function GetOne($id) {
        return DB::table($this->tables[2])
                        ->select("*")
                        ->where("ID", $id)
                        ->first();
    }

    //Lay du lieu xac dinh
    public function GetOneWithMST($mst) {
        return DB::table($this->tables[0])
                        ->select("*")
                        ->where("MST", $mst)
                        ->first();
    }

    //Luu du lieu moi
    public function SaveIn($data) {
        return DB::table($this->tables[0])
                        ->insert($data);
    }

    //Luu du lieu sua doi
    public function StoreIn($data, $id) {
        try{
        DB::table($this->tables[0])
                        ->where("ID", $id)
                        ->update($data);
        return true;
        } catch (Exception $ex) {
        return false;
        }
    }

    //Xoa du lieu xac dinh
    public function Remove($id) {
        try {
            return DB::table($this->tables[0])
                            ->delete($id);
        } catch (Exception $ex) {
            return false;
        }
    }

}
